package com.ruoyi.web.controller.api.index;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.system.domain.POrder;
import com.ruoyi.system.domain.bo.POrderBo;
import com.ruoyi.system.domain.bo.PServerTypeBo;
import com.ruoyi.system.service.IPEvaluateService;
import com.ruoyi.system.service.IPOrderService;
import com.ruoyi.system.service.IPServerTypeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/25 9:00
 * @Version: 1.0
 */

@Api(tags = {"前端-咨询订单"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/order")
public class OrderApiController {
    private final IPOrderService iPOrderService;
    private final IPServerTypeService iPServerTypeService;
    private final ISysUserService userService;
    private final IPEvaluateService evaluateService;

    @ApiOperation(value = "咨询师详情")
    @GetMapping("/user-info")
    public R<Map<String, Object>> userInfo(Long userId, String type) {
        Map<String, Object> res = new HashMap<>();
        // 咨询师下详情、用户评价
        res.put("info", userService.queryUserInfo(userId));
        res.put("evaluation", evaluateService.queryByAdviserId(userId, type));

        return R.ok(res);
    }

    @ApiOperation("查询咨询服务类型列表")
    @GetMapping("/service-list")
    public R<  Map<String, Object>> serviceList(PServerTypeBo bo) {
        return R.ok(iPServerTypeService.mApiNFO(bo.getId()));
    }

    @ApiOperation("创建咨询服务类型列表")
    @RepeatSubmit()
    @PostMapping("/add-server-type")
    public R<Void> addServerType(@Validated(AddGroup.class) @RequestBody PServerTypeBo bo) {
//        iPServerTypeService.insertFontByBo(bo);
        return R.ok("操作成功！");
    }

    @ApiOperation("修改咨询服务类型")
    @RepeatSubmit()
    @PostMapping("/update-server")
    public R<Void> updateServer(@Validated(EditGroup.class) @RequestBody PServerTypeBo bo) {
        iPServerTypeService.updateByBo(bo);
        return R.ok();
    }

    @ApiOperation("查询咨询订单列表")
    @GetMapping("/list")
    public TableDataInfo<POrder> list(POrder bo, PageQuery pageQuery) {
        return iPOrderService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("创建咨询订单")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody POrderBo bo) {
        iPOrderService.insertFontByBo(bo);
        return R.ok("操作成功！");
    }

    @ApiOperation("获取咨询订单详细信息")
    @GetMapping("/del/{id}")
    public R<POrder> getInfo(@ApiParam("主键")
                             @NotNull(message = "主键不能为空")
                             @PathVariable("id") Long id) {
        return R.ok(iPOrderService.queryById(id));
    }

    @ApiOperation("咨询支付成功-回调")
    @RepeatSubmit()
    @PostMapping("/ask-pay-ok")
    public R<Void> askPayOk(POrder bo) {
        iPOrderService.askPayOk(bo);
        return R.ok("操作成功！");
    }

    @ApiOperation("取消-咨询订单")
    @RepeatSubmit()
    @PostMapping("/cancel-order")
    public R<Void> cancelOrder(POrder bo) {
        iPOrderService.cancelOrder(bo);
        return R.ok("操作成功！");
    }

    @ApiOperation("通用-修改咨询订单状态")
    @RepeatSubmit()
    @PostMapping("/edit-status")
    public R<Void> editStatus(POrder bo) {
        iPOrderService.editStatus(bo);
        return R.ok("操作成功！");
    }

    @ApiOperation("查询已有预约咨询订单")
    @GetMapping("/has-time")
    public TableDataInfo<POrder> hasTime(POrder bo, PageQuery pageQuery) {
        pageQuery.setPageSize(100);
        return iPOrderService.queryHasTimePageList(bo, pageQuery);
    }
}
